# Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0, as
# published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an
# additional permission to link the program and your derivative works
# with the separately licensed software that they have included with
# MySQL.
#
# Without limiting anything contained in the foregoing, this file,
# which is part of MySQL Connector/C++, is also subject to the
# Universal FOSS Exception, version 1.0, a copy of which can be found at
# http://oss.oracle.com/licenses/universal-foss-exception.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA


PROJECT(MYSQLCPPCONN)

cmake_minimum_required(VERSION 2.6.2)

IF(COMMAND cmake_policy AND POLICY CMP0015)
  cmake_policy(SET CMP0015 NEW)
ENDIF(COMMAND cmake_policy AND POLICY CMP0015)

INCLUDE(VersionInfo.cmake)

OPTION(TAR_LAYOUT "Use directory layout for an unpacked TAR install")
OPTION(RPM_LAYOUT "Use directory layout for an RPM install")
OPTION(BUNDLE_DEPENDENCIES
  "Package external libraries used by the connector, such as openSSL, together
   with the connector library." FALSE)

IF(TAR_LAYOUT OR NOT CMAKE_VERSION OR CMAKE_VERSION VERSION_LESS "2.8.5")
  SET(INSTALL_LIBDIR lib)
ELSE()
  INCLUDE(GNUInstallDirs)
  SET(INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
ENDIF()

SET(EDIT_WARNING_MESSAGE "Please do not edit this file - it is generated by cmake. Edit its source file instead.")

# Configuring header file with driver version info
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/driver/version_info.h.cmake
               ${CMAKE_BINARY_DIR}/driver/version_info.h @ONLY)

# This is needed by windows installer and fro CPACK
IF(EXISTS "${CMAKE_SOURCE_DIR}/LICENSE.mysql")
  SET(LICENSE_FILENAME "${CMAKE_SOURCE_DIR}/LICENSE.mysql")
ELSE(EXISTS "${CMAKE_SOURCE_DIR}/LICENSE.mysql")
  SET(LICENSE_FILENAME "${CMAKE_SOURCE_DIR}/LICENSE")
ENDIF(EXISTS "${CMAKE_SOURCE_DIR}/LICENSE.mysql")

# Creating file with version info that will be used for installer
# We have to do this trick because of license filename that is looked in this CMAKE_SOURCE_DIR (installer's cmake is run separately)
IF(WIN32)
  FILE(WRITE "${CMAKE_BINARY_DIR}/win/config.cmake" "SET(CONNECTOR_PRODUCT_VERSION ${CONNECTOR_VERSION})\n"
                                                    "SET(LICENSE_FILENAME \"${LICENSE_FILENAME}\")\n")
ENDIF(WIN32)


# On Linux el6/el7 the default gcc is too old, see if devtoolset is installed.
# We need to look for gcc before calling PROJECT below.
OPTION(FORCE_UNSUPPORTED_COMPILER "Disable compiler version checks" OFF)
MARK_AS_ADVANCED(FORCE_UNSUPPORTED_COMPILER)

IF(UNIX)
  FIND_PROGRAM(MY_UNAME uname /bin /usr/bin /usr/local/bin /sbin)
ENDIF()

IF(CMAKE_HOST_UNIX AND NOT FORCE_UNSUPPORTED_COMPILER
    AND NOT CMAKE_C_COMPILER AND NOT CMAKE_CXX_COMPILER)
  # Cannot INCLUDE(CMakeDetermineSystem) prior to PROJECT initialization below.
  SET (ENV_CC "$ENV{CC}")
  SET (ENV_CXX "$ENV{CXX}")
  IF (ENV_CC STREQUAL "" AND ENV_CXX STREQUAL "")
    IF(MY_UNAME)
      EXEC_PROGRAM(uname ARGS -r OUTPUT_VARIABLE MY_HOST_SYSTEM_VERSION)
      IF(MY_HOST_SYSTEM_NAME MATCHES "Linux" AND
          MY_HOST_SYSTEM_VERSION MATCHES "\\.el[67](uek)?\\.")
        MESSAGE(STATUS
          "This is ${MY_HOST_SYSTEM_NAME} version ${MY_HOST_SYSTEM_VERSION}")
        FIND_PROGRAM(MY_GCC_COMPILER gcc)
        IF (MY_GCC_COMPILER)
          MESSAGE(STATUS "gcc is ${MY_GCC_COMPILER}")
          MESSAGE(STATUS
            "We probably need /opt/rh/devtoolset-6/root/usr/bin/gcc")
          EXECUTE_PROCESS(COMMAND ${MY_GCC_COMPILER} -dumpversion
            OUTPUT_VARIABLE MY_GCC_VERSION)
          IF(MY_GCC_VERSION VERSION_LESS 5.0)
            MESSAGE(STATUS "${MY_GCC_COMPILER} is version ${MY_GCC_VERSION}")
            FIND_PROGRAM(ALTERNATIVE_GCC gcc
              NO_DEFAULT_PATH
              PATHS "/opt/rh/devtoolset-6/root/usr/bin")
            FIND_PROGRAM(ALTERNATIVE_GPP g++
              NO_DEFAULT_PATH
              PATHS "/opt/rh/devtoolset-6/root/usr/bin")
            IF(ALTERNATIVE_GCC AND ALTERNATIVE_GPP)
              SET(CMAKE_C_COMPILER ${ALTERNATIVE_GCC})
              SET(CMAKE_CXX_COMPILER ${ALTERNATIVE_GPP})
              MESSAGE(STATUS "Using ${ALTERNATIVE_GCC}")
              MESSAGE(STATUS "Using ${ALTERNATIVE_GPP}")
            ELSE()
              MESSAGE(WARNING "Could not find devtoolset gcc")
            ENDIF()
          ENDIF()
        ENDIF()
      ENDIF()
    ENDIF()
  ENDIF()
ENDIF()

#-----------------
# CPPFLAGS, CXXFLAGS and LDFLAGS from the environment

SET(FreeBSD11Up False)
if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" AND
    ${CMAKE_SYSTEM_VERSION} GREATER "10")
  SET(FreeBSD11Up True)
endif()

# c++11 enabled by default only for FreeBSD 11 and up
OPTION(CMAKE_ENABLE_C++11 "Build Connector using c++11" ${FreeBSD11Up})
IF(CMAKE_ENABLE_C++11)
  INCLUDE(CheckCXXCompilerFlag)
  CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
  CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
  IF(COMPILER_SUPPORTS_CXX11)
    MESSAGE(STATUS "Compiler supports C++11. Adding -std=c++11 flags")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  ELSEIF(COMPILER_SUPPORTS_CXX0X)
    MESSAGE(STATUS "Compiler supports C++11. Adding -std=c++0x flag.")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
  ELSE()
    MESSAGE(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} doesn't support C++11. Please use a different compiler.")
  ENDIF(COMPILER_SUPPORTS_CXX11)
ENDIF(CMAKE_ENABLE_C++11)


#
# Configure static runtime library on Windows if requested
#

option(STATIC_MSVCRT "Use static MSVC runtime library" OFF)

if(WIN32)
  IF(STATIC_MSVCRT)

    message("Using static runtime library")

    foreach(LANG C CXX)
      set(CMAKE_${LANG}_FLAGS "${CMAKE_${LANG}_FLAGS} /MT")
      foreach(TYPE RELEASE RELWITHDEBINFO MINSIZEREL)
        set(CMAKE_${LANG}_FLAGS_${TYPE} "${CMAKE_${LANG}_FLAGS_${TYPE}} /MT")
      endforeach()
      set(CMAKE_${LANG}_FLAGS_DEBUG "${CMAKE_${LANG}_FLAGS_DEBUG} /MTd")
    endforeach(LANG)
 ELSE(STATIC_MSVCRT)
    foreach(LANG C CXX)
      set(CMAKE_${LANG}_FLAGS "${CMAKE_${LANG}_FLAGS} /MD")
      foreach(TYPE RELEASE RELWITHDEBINFO MINSIZEREL)
        set(CMAKE_${LANG}_FLAGS_${TYPE} "${CMAKE_${LANG}_FLAGS_${TYPE}} /MD")
      endforeach()
      set(CMAKE_${LANG}_FLAGS_DEBUG "${CMAKE_${LANG}_FLAGS_DEBUG} /MDd")
    endforeach(LANG)
 ENDIF(STATIC_MSVCRT)

endif()


#-----------------
# ICU

SET(MYSQLCPPCONN_ICU_ENABLE 0 CACHE BOOL "development only: search icu although we do not make use of it yet.")

IF(MYSQLCPPCONN_ICU_ENABLE)
  SET(MYSQLCPPCONN_ICU_CONFIG_EXECUTABLE)
  SET(MYSQLCPPCONN_ICU_LIBRARY)
  SET(MYSQLCPPCONN_ICU_INCLUDE)

  FIND_PROGRAM (MYSQLCPPCONN_ICU_CONFIG_EXECUTABLE icu-config)

  IF (NOT MYSQLCPPCONN_ICU_CONFIG_EXECUTABLE)
    SET (MYSQLCPPCONN_ICU_FOUND FALSE)
    MESSAGE(STATUS "icu-config not found")
  ELSE (NOT MYSQLCPPCONN_ICU_CONFIG_EXECUTABLE)

    EXEC_PROGRAM ("${MYSQLCPPCONN_ICU_CONFIG_EXECUTABLE} --ldflags-libsonly"
    OUTPUT_VARIABLE MYSQLCPPCONN_ICU_LIBRARY
    RETURN_VALUE ERROR_CODE
    )

    EXEC_PROGRAM ("${MYSQLCPPCONN_ICU_CONFIG_EXECUTABLE} --cppflags-searchpath|sed s/^-I//"
    OUTPUT_VARIABLE MYSQLCPPCONN_ICU_INCLUDE
    RETURN_VALUE ERROR_CODE
    )

    EXEC_PROGRAM ("${MYSQLCPPCONN_ICU_CONFIG_EXECUTABLE} --ldflags-searchpath|sed s/^-L//"
    OUTPUT_VARIABLE MYSQLCPPCONN_ICU_LDLIB
    RETURN_VALUE ERROR_CODE
    )

    IF (MYSQLCPPCONN_ICU_LIBRARY)
      SET (MYSQLCPPCONN_ICU_FOUND TRUE)
    ENDIF (MYSQLCPPCONN_ICU_LIBRARY)

    MESSAGE(STATUS "ICU::CONFIG: ${MYSQLCPPCONN_ICU_CONFIG_EXECUTABLE}")
    MESSAGE(STATUS "ICU::LIBRARY: ${MYSQLCPPCONN_ICU_LIBRARY}")
    MESSAGE(STATUS "ICU::INCLUDE: ${MYSQLCPPCONN_ICU_INCLUDE}")
    MESSAGE(STATUS "ICU::LDLIB: ${MYSQLCPPCONN_ICU_LDLIB}")

    INCLUDE_DIRECTORIES(${MYSQLCPPCONN_ICU_INCLUDE})
    LINK_DIRECTORIES(${MYSQLCPPCONN_ICU_LDLIB})
  ENDIF (NOT MYSQLCPPCONN_ICU_CONFIG_EXECUTABLE)
ENDIF(MYSQLCPPCONN_ICU_ENABLE)
#-----------------

#-----------------
# BOOST
SET(ENV_BOOST $ENV{BOOST_ROOT})
IF(NOT BOOST_ROOT AND ENV_BOOST)
  SET(BOOST_ROOT ${ENV_BOOST})
ENDIF(NOT BOOST_ROOT AND ENV_BOOST)

IF(NOT BOOST_ROOT AND WIN32)
  SET(BOOST_ROOT "C:/Program Files/Boost")
ENDIF(NOT BOOST_ROOT AND WIN32)

# Prefer static linking in all cases
SET(Boost_ADDITIONAL_VERSIONS "1.37.0" "1.37" "1.38.0" "1.38" "1.39.0" "1.39")
SET(Boost_NO_BOOST_CMAKE ON CACHE BOOL "Enable fix for FindBoost.cmake")
SET(MYSQLCPPCONN_BOOST_COMPONENTS thread date_time)
SET(Boost_USE_STATIC_LIBS TRUE)
#FIND_PACKAGE(Boost COMPONENTS ${MYSQLCPPCONN_BOOST_COMPONENTS})
FIND_PACKAGE(Boost)
IF(NOT Boost_FOUND)
  # Try dynamic
  SET(Boost_USE_STATIC_LIBS FALSE)
  FIND_PACKAGE(Boost COMPONENTS ${MYSQLCPPCONN_BOOST_COMPONENTS})
  IF(NOT Boost_FOUND)
    MESSAGE(FATAL_ERROR "Boost or some of its libraries found. If not in standard place please set -DBOOST_ROOT:STRING=")
  ENDIF(NOT Boost_FOUND)
ENDIF(NOT Boost_FOUND)

SET(MYSQLCPPCONN_BOOST_INCLUDE_DIRS ${Boost_INCLUDE_DIRS})
SET(MYSQLCPPCONN_BOOST_SYSTEM_LIBS ${Boost_SYSTEM_LIBRARY})
SET(MYSQLCPPCONN_BOOST_THREAD_LIBS ${Boost_THREAD_LIBRARY})
SET(MYSQLCPPCONN_BOOST_LIBRARY_DIRS ${Boost_LIBRARY_DIRS})

INCLUDE_DIRECTORIES(${MYSQLCPPCONN_BOOST_INCLUDE_DIRS})
MESSAGE(STATUS "BOOST_INCLUDE_DIRS=${MYSQLCPPCONN_BOOST_INCLUDE_DIRS}")


#-----------------
IF(COMMAND cmake_policy)
  cmake_policy(SET CMP0003 NEW)
ENDIF(COMMAND cmake_policy)

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckTypeSize)


#----------------------
# STLPORT - BEGIN
#
SET(MYSQLCPPCONN_STLPORT_ENABLE 0 CACHE BOOL "stlport-enabled")
IF(MYSQLCPPCONN_STLPORT_ENABLE)
  MESSAGE(STATUS "Looking for STLPort")
  FIND_PATH(STLPORT_INCLUDE_DIR stl_user_config.h
            $ENV{STLPORT_INCLUDE_DIR}
            /usr/include/stlport
            /usr/local/include/stlport)
  IF(STLPORT_INCLUDE_DIR)
    MESSAGE(STATUS "Using STLPort from : ${STLPORT_INCLUDE_DIR}")

    INCLUDE_DIRECTORIES(STLPORT_INCLUDE_DIR)
    SET(MYSQLCPPCONN_STLPORT_LIB "stlport")
  ELSE(STLPORT_INCLUDE_DIR)
    MESSAGE(FATAL_ERROR "STLPORT not found. Please set \$ENV{STLPORT_INCLUDE_DIR} if installed in non-standard location")
  ENDIF(STLPORT_INCLUDE_DIR)

  # We want to set the inclusion of the library globally
  ADD_DEFINITIONS(-library=stlport4) # Not a macro defintion, but CMake manual says I can!! ;)
  SET(CMAKE_EXE_LINKER_FLAGS        "${CMAKE_EXE_LINKER_FLAGS} -library=stlport4")
  SET(CMAKE_SHARED_LINKER_FLAGS     "${CMAKE_SHARED_LINKER_FLAGS} -library=stlport4")
  SET(CMAKE_MODULE_LINKER_FLAGS     "${CMAKE_MODULE_LINKER_FLAGS} -library=stlport4")

ELSE(MYSQLCPPCONN_STLPORT_ENABLE)
  SET(MYSQLCPPCONN_STLPORT_LIB "")
ENDIF(MYSQLCPPCONN_STLPORT_ENABLE)
#
# STLPORT - END
#----------------------

# Make tests cover methods that throw not implemented to detect API changes?
SET(MYSQLCPPCONN_TEST_NOT_IMPLEMENTED 0 CACHE BOOL "HEAD/trunk QA: invoke methods that should return not implemented to detect API changes")

INCLUDE(${CMAKE_SOURCE_DIR}/FindMySQL.cmake)

SET(MYSQLCLIENT_STATIC_BINDING 1 CACHE BOOL "enable static binding")
IF(MYSQLCLIENT_STATIC_BINDING AND EXISTS "${CMAKE_SOURCE_DIR}/cmake/mysql_version_info.cmake")
  INCLUDE(${CMAKE_SOURCE_DIR}/cmake/mysql_version_info.cmake)
  IF(NOT LIBMYSQL_CPP_VERSION STREQUAL "")
    SET(MYSQL_VERSION "${LIBMYSQL_CPP_VERSION}")
  ELSEIF(NOT MYSQL_CPP_SERVER_VERSION STREQUAL "")
    SET(MYSQL_VERSION "${MYSQL_CPP_SERVER_VERSION}")
  ENDIF(NOT LIBMYSQL_CPP_VERSION STREQUAL "")
  STRING(REGEX MATCHALL "([0-9]+.[0-9]+.[0-9]+)" MYSQL_VERSION "${MYSQL_VERSION}")

  IF(LIBMYSQL_CPP_VERSION_ID)
    SET(MYSQL_NUM_VERSION ${LIBMYSQL_CPP_VERSION_ID})
  ELSEIF(MYSQL_CPP_SERVER_VERSION_ID)
    SET(MYSQL_NUM_VERSION ${MYSQL_CPP_SERVER_VERSION_ID})
  ENDIF(LIBMYSQL_CPP_VERSION_ID)
ENDIF()

IF(MYSQLCLIENT_STATIC_BINDING AND
  (NOT CMAKE_CXX_COMPILER_ID STREQUAL MYSQL_COMPILER_ID OR
    NOT CMAKE_CXX_COMPILER_VERSION STREQUAL MYSQL_COMPILER_VERSION
    )
  )
  MESSAGE (WARNING "libmysqlclient and connector compilers mismatch:\n"
                   "libmysqlclient: ${MYSQL_COMPILER_ID} ${MYSQL_COMPILER_VERSION}\n"
                   "connector: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}.")
ENDIF()


OPTION(USE_SERVER_CXXFLAGS "USE MYSQL_CXXFLAGS on connector build" TRUE)

message(STATUS "USE_SERVER_FLAGS : ${USE_SERVER_CXXFLAGS}")
if (USE_SERVER_CXXFLAGS)
  SET(MYSQLCPPCONN_COMPILE_FLAGS_ENV "$ENV{CPPFLAGS} ${MYSQL_CXXFLAGS} $ENV{CXXFLAGS}")
else (USE_SERVER_CXXFLAGS)
  SET(MYSQLCPPCONN_COMPILE_FLAGS_ENV "$ENV{CPPFLAGS} $ENV{CXXFLAGS}")
endif(USE_SERVER_CXXFLAGS)

MESSAGE(STATUS "Environment compile flags: ${MYSQLCPPCONN_COMPILE_FLAGS_ENV}")

SET(MYSQLCPPCONN_LINK_FLAGS_ENV "$ENV{LDFLAGS}")
MESSAGE(STATUS "Environment link flags: ${MYSQLCPPCONN_LINK_FLAGS_ENV}")

# Configuring header file with DM version info
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/cppconn/version_info.h.cmake
               ${CMAKE_BINARY_DIR}/cppconn/version_info.h @ONLY)

#
# ----------------------------------------------------------------------
# Create package script
# ----------------------------------------------------------------------

IF(NOT CONNECTOR_PLATFORM)
  IF(WIN32)
    IF(CMAKE_SIZEOF_VOID_P MATCHES 8)
      SET(CONNECTOR_PLATFORM "winx64")
    ELSE(CMAKE_SIZEOF_VOID_P MATCHES 8)
      SET(CONNECTOR_PLATFORM "win32")
    ENDIF(CMAKE_SIZEOF_VOID_P MATCHES 8)
  ELSE(WIN32)
    SET(CONNECTOR_PLATFORM "unknown")
  ENDIF(WIN32)
ENDIF(NOT CONNECTOR_PLATFORM)

#CPACK version variables are initialized in VersionInfo.cmake
# Get the part of the package name for this product
IF(MYSQL_SERVER_SUFFIX STREQUAL "-community")
  SET(CPACK_SERVER_SUFFIX "")
ELSE(MYSQL_SERVER_SUFFIX STREQUAL "-community")
  SET(CPACK_SERVER_SUFFIX ${MYSQL_SERVER_SUFFIX})
ENDIF(MYSQL_SERVER_SUFFIX STREQUAL "-community")

IF(EXTRA_NAME_SUFFIX)
  SET(CPACK_PACKAGE_NAME "mysql-connector-c++${EXTRA_NAME_SUFFIX}")
ELSE(EXTRA_NAME_SUFFIX)
  SET(CPACK_PACKAGE_NAME "mysql-connector-c++")
ENDIF(EXTRA_NAME_SUFFIX)

SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY
    "Connector/C++, a library for connecting to MySQL servers.")
SET(CPACK_PACKAGE_VENDOR "Oracle and/or its affiliates")
SET(CPACK_RPM_PACKAGE_DESCRIPTION
    "The MySQL Connector/C++ is a MySQL database connector for C++. The
    MySQL Driver for C++ can be used to connect to the MySQL Server from
    C++ applications. The MySQL Driver for C++ mimics the JDBC 4.0 API. It
    is recommended to use the connector with MySQL 5.1 or later. Note -
    its full functionality is not available when connecting to MySQL 5.0.
    The MySQL Driver for C++ cannot connect to MySQL 4.1 or earlier. MySQL
    is a trademark of ${CPACK_PACKAGE_VENDOR}

    The MySQL software has Dual Licensing, which means you can use the MySQL
    software free of charge under the GNU General Public License
    (http://www.gnu.org/licenses/). You can also purchase commercial MySQL
    licenses from ${CPACK_PACKAGE_VENDOR} if you do not wish to be
    QLCPPCONN_VERSION
    in the manual for further info.")

SET(CPACK_RESOURCE_FILE_LICENSE "${LICENSE_FILENAME}")
SET(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README")
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${MYSQLCPPCONN_VERSION}")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "${CPACK_PACKAGE_NAME}-${MYSQLCPPCONN_VERSION}-${CONNECTOR_PLATFORM}")
IF(WIN32)
  SET(CPACK_GENERATOR "ZIP")
  SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-noinstall-${MYSQLCPPCONN_VERSION}-${CONNECTOR_PLATFORM}")
ELSE(WIN32)
  SET(CPACK_GENERATOR "TGZ")
  SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY}")
ENDIF(WIN32)

SET(DOC_DESTINATION ".")
IF(RPM_LAYOUT)
  SET(DOC_DESTINATION "share/doc/${CPACK_PACKAGE_NAME}-${MYSQLCPPCONN_VERSION}")
ENDIF()

INSTALL(FILES
  ${CPACK_RESOURCE_FILE_README}
  ${CPACK_RESOURCE_FILE_LICENSE}
  DESTINATION ${DOC_DESTINATION} OPTIONAL)

SET(COMMON_IGNORE_FILES
        "/CMakeFiles/"
        "/Testing/"
        "/.bzr/"
        "_CPack_Packages/"
        "~"
        ".swp"
        ".log"
        ".gz"
        ".directory$"
        "CMakeCache.txt"
        "Makefile"
  "install_manifest.txt"
)
SET(PRJ_COMMON_IGNORE_FILES
        ${COMMON_IGNORE_FILES}
  "ANNOUNCEMENT_102_ALPHA"
  "ANNOUNCEMENT_103_ALPHA"
  "ANNOUNCEMENT_104_BETA"
  "ANNOUNCEMENT_105_GA"
  "ANNOUNCEMENT_110_GA"
  "ANNOUNCEMENT_111_GA"
  "ANNOUNCEMENT_DRAFT"
)

SET(CPACK_SOURCE_IGNORE_FILES
  ${PRJ_COMMON_IGNORE_FILES}
  "cppconn/config.h$"
  "cppconn/version_info.h$"
  "driver/nativeapi/binding_config.h$"
  "driver/version_info.h$"
)
SET(CPACK_PACKAGE_IGNORE_FILES ${PRJ_COMMON_IGNORE_FILES} "something_there" )
SET(CPACK_SOURCE_GENERATOR "TGZ")

SET(MYSQLCPPCONN_GCOV_ENABLE 0 CACHE BOOL "gcov-enabled")
IF(CMAKE_COMPILER_IS_GNUCC)
  ADD_DEFINITIONS("-Wall -fPIC -Woverloaded-virtual")
  IF (MYSQLCPPCONN_GCOV_ENABLE)
    ADD_DEFINITIONS("-fprofile-arcs -ftest-coverage")
  ENDIF (MYSQLCPPCONN_GCOV_ENABLE)
ENDIF(CMAKE_COMPILER_IS_GNUCC)



# SET(CPACK_*) before the INCLUDE(CPack)
INCLUDE(CPack)

IF(WIN32)
  STRING(REGEX REPLACE "MYSQLCPPCONN" "MySQL/ConnectorCPP" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
ENDIF(WIN32)
MESSAGE(STATUS "Installation path is: ${CMAKE_INSTALL_PREFIX}   (overwrite with -DCMAKE_INSTALL_PREFIX=/your/path)")

INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}/cppconn)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/cppconn)
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}/driver/nativeapi)

ADD_SUBDIRECTORY(cppconn)
ADD_SUBDIRECTORY(driver)
ADD_SUBDIRECTORY(examples)
ADD_SUBDIRECTORY(test)
ADD_SUBDIRECTORY(test/framework)
ADD_SUBDIRECTORY(test/CJUnitTestsPort)
ADD_SUBDIRECTORY(test/unit)

IF(DEFINED CMAKE_SYSTEM_NAME AND ${CMAKE_SYSTEM_NAME} STREQUAL "SunOS")
    # see also README
  IF(${CMAKE_C_COMPILER} MATCHES "gcc$")
    MESSAGE("NOTE")
    MESSAGE("You seem to be using GCC on SunOS.")
    MESSAGE("This is not recommended. Compilation")
    MESSAGE("might fail due to compile flags")
    MESSAGE("returned by the MySQL tool mysql_config.")
    MESSAGE("In case of an error, try commenting out:")
    MESSAGE("# ADD_DEFINITIONS(\"\${MYSQL_CXXFLAGS}\")")
    MESSAGE("in the file FindMySQL.cm")
  ENDIF(${CMAKE_C_COMPILER} MATCHES "gcc$")

  IF(${CMAKE_C_COMPILER} MATCHES "gcc$")
    IF(${CMAKE_CXX_COMPILER} MATCHES "CC$")
      MESSAGE("NOTE")
      MESSAGE("You seem to be using the GCC C-compiler")
      MESSAGE("together with the Sun CC C++-compiler.")
      MESSAGE("Linking of the resulting binaries")
      MESSAGE("might fail. In case of an error,")
      MESSAGE("try using Sun compilers only")
    ENDIF(${CMAKE_CXX_COMPILER} MATCHES "CC$")
  ELSEIF(${CMAKE_C_COMPILER} MATCHES "cc$" AND ${CMAKE_CXX_COMPILER} MATCHES "[c|g]\\+\\+$")
    MESSAGE("NOTE")
    MESSAGE("You seem to be using the GCC C-compiler")
    MESSAGE("together with the Sun CC C++-compiler.")
    MESSAGE("Linking of the resulting binaries")
    MESSAGE("might fail. In case of an error,")
    MESSAGE("try using Sun compilers only")
  ENDIF(${CMAKE_C_COMPILER} MATCHES "gcc$")
ENDIF(DEFINED CMAKE_SYSTEM_NAME AND ${CMAKE_SYSTEM_NAME} STREQUAL "SunOS")


#
# Get locations of the openSSL libraries (resolving links on Unix) and
# store them in OPENSSL_FILES global variable.
#

function(bundle_ssl_libs)
  MESSAGE(STATUS "Finding SSL libraries on ${MYSQL_LIB_DIR}")
  if(WIN32)

    install(DIRECTORY "${MYSQL_DIR}/bin/" DESTINATION ${INSTALL_LIBDIR}
     FILES_MATCHING REGEX "(.*eay.*)")

  else()

    find_library(_lib_ssl
      NAMES
        libssl ssl
      PATHS
        "${MYSQL_LIB_DIR}/"
      NO_DEFAULT_PATH
      NO_CMAKE_ENVIRONMENT_PATH
    )

    find_library(_lib_crypto
      NAMES
        libcrypto crypto
      PATHS
        "${MYSQL_LIB_DIR}/"
      NO_DEFAULT_PATH
      NO_CMAKE_ENVIRONMENT_PATH
    )
    set(SSL_LIBS_PACKAGE "")
    list(APPEND SSL_LIBS_PACKAGE "${_lib_ssl}" "${_lib_crypto}" )

  foreach(lib ${SSL_LIBS_PACKAGE})

    MESSAGE(STATUS "Packaging ${lib}")

    get_filename_component(lib_realpath "${lib}"  REALPATH)

    install(FILES ${lib} ${lib_realpath} DESTINATION ${INSTALL_LIBDIR} OPTIONAL)

  endforeach()

    #install(DIRECTORY "${MYSQL_LIB_DIR}" DESTINATION ${INSTALL_LIBDIR}
    # FILES_MATCHING REGEX "(.*crypt.*)|(.*ssl.*)" EXCLUDE )

  endif()

endfunction()

if(BUNDLE_DEPENDENCIES)
  message(STATUS "Bundling OpenSSL libraries")
  bundle_ssl_libs()
endif()

# Create the BUILDINFO file
# =========================
#
include(buildinfo.cmake)
